//
//  MTVerificationWaitForResultViewController.swift
//  MT
//
//  Created by M on 2019/12/24.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory
import YPNavigationBarTransition

// 网络
import RxSwift
import RxDataSources
import Moya
import Result

class MTVerificationWaitForResultViewController: BaseViewController {

    /// MTProvider
    private lazy var provider: MTProvider<UserCenterApi> = {
        return MTProvider<UserCenterApi>(plugins: [])
    }()
    private let disposeBag = DisposeBag()
    
    /// 标题文字
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "我们正在加速认证中\n请喝杯咖啡耐心等待"
        label.font = PingFangSCRegularFont(18)
        label.textColor = .white
        label.textAlignment = .center
        label.numberOfLines = 2
        return label
    }()
    
    /// 认证人数 Label
    lazy var countTitleLabel: UILabel = {
        let label = UILabel()
        label.text = "前方认证人数"
        label.font = PingFangSCRegularFont(18)
        label.textColor = UIColor.theme.main
        label.textAlignment = .center
        return label
    }()
    
    /// 人数 Label
    lazy var countLabel: UILabel = {
        let label = UILabel()
        label.text = "-"
        label.font = PingFangSCSemiboldFont(60)
        label.textColor = UIColor.theme.main
        label.textAlignment = .center
        return label
    }()
    
    /// tips
    lazy var tipsLabel: UILabel = {
        let label = UILabel()
        label.text = "如有疑问，请关注微信公众号咨询\n感谢您对荼蘼的支持！"
        label.font = PingFangSCRegularFont(12)
        label.textColor = .white
        label.textAlignment = .center
        label.numberOfLines = 2
        return label
    }()
    
}

// MARK: ------------------ Overrides ----------------------
extension MTVerificationWaitForResultViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initializeUI()
        requestForAuditStatus()
    }
        
    override func yp_navigationBarTintColor() -> UIColor! {
        UIColor(hex: 0xFFFFFF)
    }

    override func yp_navigationBackgroundColor() -> UIColor! {
        .clear
    }

    override func yp_navigationBackgroundImage(withIdentifier identifier: AutoreleasingUnsafeMutablePointer<NSString?>!) -> UIImage! {
        return nil
    }
    
    override func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        .backgroundStyleTransparent
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MTVerificationWaitForResultViewController {
    
    private func initializeUI() {
        navigationItem.leftBarButtonItem = nil
        view.backgroundColor = UIColor.theme.background
        view.addSubview(titleLabel)
        view.addSubview(countTitleLabel)
        view.addSubview(countLabel)
        view.addSubview(tipsLabel)
    
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(100)
            make.centerX.equalTo(view)
        }
        
        let i1 = createImageView(named: "icon_user1")
        i1.snp.makeConstraints { (make) in
            make.center.equalTo(view)
            make.width.equalTo(30)
            make.height.equalTo(36)
        }
        let i2 = createImageView(named: "icon_user2")
        i2.snp.makeConstraints { (make) in
            make.left.equalTo(i1.snp.right).offset(15)
            make.centerY.equalTo(i1)
            make.width.equalTo(30)
            make.height.equalTo(36)
        }
        let i2_1 = createImageView(named: "icon_user3")
        i2_1.snp.makeConstraints { (make) in
            make.left.equalTo(i2.snp.right).offset(15)
            make.centerY.equalTo(i1)
            make.width.equalTo(30)
            make.height.equalTo(36)
        }
        let i3 = createImageView(named: "icon_user2")
        i3.snp.makeConstraints { (make) in
            make.right.equalTo(i1.snp.left).offset(-15)
            make.centerY.equalTo(i1)
            make.width.equalTo(30)
            make.height.equalTo(36)
        }
        let i3_1 = createImageView(named: "icon_user3")
        i3_1.snp.makeConstraints { (make) in
            make.right.equalTo(i3.snp.left).offset(-15)
            make.centerY.equalTo(i1)
            make.width.equalTo(30)
            make.height.equalTo(36)
        }
        
        countLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(i1.snp.top).offset(-16)
            make.centerX.equalTo(view)
        }
        
        countTitleLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(countLabel.snp.top).offset(5)
            make.centerX.equalTo(view)
        }
        
        tipsLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(view).offset(-ScreenBottomExtraOffset - 32)
            make.centerX.equalTo(view)
        }
    }
    
}

// MARK: ------------------ 数据请求 ----------------------
extension MTVerificationWaitForResultViewController {
    
    private func requestForAuditStatus() {
        provider.rx.originRequest(.auditStatus).subscribe(onNext: { [weak self] (result, isCache) in
            
            guard let weakSelf = self else { return }
            switch result {
                
            case let .success(response):
                if let datas = try? response.mapJSON() as? [String: Any], let code = datas["code"] as? Int {
                    if code == 200, let dict = datas["result"] as? [String:Any], let userStatus = dict["userStatus"] as? Int {
                        /// 更新 userstatus
                        UserDefaults.MTUserCenter.userStatus.store(value: userStatus)
                        UserCenter.default.userStatus = userStatus
                        if userStatus == 12, let count = dict["count"] as? Int {
                            DispatchQueue.main.async {
                                weakSelf.countLabel.text = "\(count)"
                            }
                        } else {
                            UserStatusNavigate.push(from: weakSelf, withUserStatus: userStatus)
                        }
                    } else {
                        weakSelf.requestForAuditStatus()
                    }
                }
                break
                
            case .failure:
                weakSelf.requestForAuditStatus()
                break
                
            }
            
        }).disposed(by: disposeBag)
    }
    
}

// MARK: ------------------ 其它 ----------------------
extension MTVerificationWaitForResultViewController {
    
    private func createImageView(named: String) -> UIImageView {
        let imgView = UIImageView()
        imgView.image = UIImage(named: named)
        imgView.contentMode = .scaleAspectFill
        self.view.addSubview(imgView)
        return imgView
    }
    
}
